package mi.payment.utils.transaction;

import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.utils.OrderJudgmentUtil;
import mi.payment.utils.SharedPreferencesUtil;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 轮询Task
 *
 * @author Created by yiuhet on 2017/8/7.
 */
public class PollingTask extends BaseCountDownTask<TradeData, Void, TradeData> {

    private static final String TAG = "PollingTask";

    private int interval = 5;

    private volatile boolean forceStop;

    /**
     * 设置轮询间隔
     */
    public void setInterval(int interval) {
        this.interval = interval;
    }

    public void setPollingCount(int count) {
        this.count = count;
    }

    private int count; // 查询次数
    private TransactionTask<TradeData, Void, TradeData> task; // 查询task

    public PollingTask(int count, TransactionTask<TradeData, Void, TradeData> task) {
        this.count = count;
        this.task = task;
    }

    @Override
    protected void onPreExecuteImpl() {
        super.onPreExecuteImpl();
        LogUtil.e(TAG, "PollingTask onPreExecuteImpl()");
    }

    @Override
    protected TradeData doInBackgroundImpl(TradeData... params) {
        TradeData reqTradeData = null;
        LogUtil.e(TAG, "进入轮询task,最大次数为：" + count);
        for (int i = 0; i < count; i++) {
            if (isTaskTimeout()) {
                LogUtil.e(TAG, "任务超时，结束轮询task。。。");
                break;
            }
            if (forceStop) {
                LogUtil.e(TAG, "任务被强制终止，结束轮询task。。。");
                break;
            }
            reqTradeData = SharedPreferencesUtil.clone(params[0]);
            final PayDetail payDetail = reqTradeData.getPayDetail();
            SystemParamsOperation systemParamsOperation = SystemParamsOperation.getInstance();
            String serialNum = systemParamsOperation.getIncrementSerialNum();
            LogUtil.e(TAG, "queryResult 流水: " + serialNum);
            payDetail.setVoucherNo(serialNum);

            LogUtil.e(TAG, "正在第" + i + "次轮询。。。");
            task.syncExecute(reqTradeData);
            LogUtil.e(TAG, "第" + i + "次轮询结束,状态为：" + task.getTransactionResult());

            boolean unknownOrder = OrderJudgmentUtil.isQRUnknownOrder(payDetail);
            if (unknownOrder) {
                LogUtil.e(TAG, "状态不明。。。");
            } else {
                LogUtil.e(TAG, "轮询收到结果，轮询task结束。。。");
                return reqTradeData;
            }
            try {
                Thread.sleep(interval * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        LogUtil.e(TAG, "指定次数轮询没查询到，直接return。。。");
        return reqTradeData;
    }

    @Override
    protected void onPostExecuteImpl(TradeData tradeData) {
        super.onPostExecuteImpl(tradeData);
        LogUtil.e(TAG, "收到结束标志，结束轮询task。。。");
    }

    /**
     * 强制终止任务
     */
    public void forceStopPolling() {
        forceStop = true;
        cancel(true);
    }

}

